package com.wwdablu.soumya.lottiebottomnav;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.value.LottieAnimationViewData;
import com.wwdablu.soumya.lottiebottomnav.util.LogUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * LottieViewCreator
 *
 * @since 2021-05-19
 */
public class LottieViewCreator extends DirectionalLayout {
    Text lmiMenuText;
    LottieAnimationView lmiMenuItem;
    DirectionalLayout menuContainer;

    /**
     * LottieViewCreator
     *
     * @param context
     */
    public LottieViewCreator(Context context) {
        this(context, null);
    }

    /**
     * LottieViewCreator
     *
     * @param context
     * @param attrSet
     */
    public LottieViewCreator(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * LottieViewCreator
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public LottieViewCreator(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        Component rootView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_lottie_menu_item, this, false);
        lmiMenuText = (Text) rootView.findComponentById(ResourceTable.Id_lmi_menu_text);
        lmiMenuItem = (LottieAnimationView) rootView.findComponentById(ResourceTable.Id_lmi_menu_item);
        menuContainer = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_menu_container);
        addComponent(rootView);
    }

    /**
     * getLottieAnimationView
     *
     * @param lmiMenuItem
     */
    public void getLottieAnimationView(LottieAnimationView lmiMenuItem){
        this.lmiMenuItem = lmiMenuItem;
    }

    /**
     * from
     *
     * @param parent
     * @param menuItem
     * @param isSelected
     * @param config
     */
    public void from(ComponentContainer parent,
                     MenuItem menuItem,
                     boolean isSelected,
                     Config config) {
        lmiMenuText.setFont(menuItem.fontItem.getTypeface());
        lmiMenuText.setText(menuItem.fontItem.getSpannableTitle());

        lmiMenuText.setTextSize(isSelected ? menuItem.fontItem.getSelectedTextSize()
                : menuItem.fontItem.getUnselectedTextSize(), Text.TextSizeType.FP);

        lmiMenuText.setTextColor(isSelected ? new Color(menuItem.fontItem.getTextSelectedColor()) :
                new Color(menuItem.fontItem.getTextUnselectedColor()));

        setLottieView(lmiMenuItem, menuItem, isSelected);
        ComponentContainer.LayoutConfig params = lmiMenuItem.getLayoutConfig();
        params.width = isSelected ? config.getSelectedMenuWidth() : config.getUnselectedMenuWidth();
        params.height = isSelected ? config.getSelectedMenuHeight() : config.getUnselectedMenuHeight();
        lmiMenuItem.setLayoutConfig(params);
        LogUtil.error("LottieViewCreator",menuItem.fontItem.getSpannableTitle());
//        if (!config.isShowTextOnUnselected()) {
//            lmiMenuText.setVisibility(isSelected ? Component.VERTICAL : Component.INVISIBLE);
//        }
    }

    private static void setLottieView(LottieAnimationView view, MenuItem menuItem, boolean isSelected) {
        LottieAnimationViewData data = new LottieAnimationViewData();
        data.setFilename(isSelected ? menuItem.selectedLottieName : menuItem.unselectedLottieName);
        data.autoPlay = true;
        data.repeatCount = menuItem.loop ? Integer.MAX_VALUE : 0; // specify repetition count
        view.setAnimationData(data);
        view.setProgress(menuItem.lottieProgress);
        view.setSpeed(0f);
    }
}
